package com.fingard.dsp.bank.directbank.egbank01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class EGBANK01Balance extends EGBANK01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            EGBANK01Header bankHeader = new EGBANK01Header();
            bankHeader.opName = "queryBalanceOp";
            bankHeader.needSigned = false;
            while (p_req7506.readRecord()) {
                bankHeader.serialNo = getQryTransID();

                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeElementString("cstNo", bankFront.loginName);
                xmlWriter.writeStartElement("reqParam");
                xmlWriter.writeElementString("ACNO", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("BIZH", "");
                xmlWriter.writeEndDocument();

                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                tmpRetDetail.actNum = p_req7506.readedDetail.actNum;

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/HFBankData/opRep/retCode");
                        String tmpRetInfo = tmpBkReader.getFirstText("/HFBankData/opRep/errMsg");
                        if (tmpRetCode != null && tmpRetCode.equals("0")) {
                            if (tmpBkReader.read("/HFBankData/opRep/opResult")) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
                                tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("ACNO");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AVBL");
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("CYER");
                                tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("FAMT");
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpRetInfo;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
